﻿@using PagedList.Mvc;

@model PagedList.IPagedList<Product>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@{
    Trend trend = ViewBag.Trend;
    string search = ViewBag.Search;
    string sortOrder = ViewBag.SortOrder;
    int pageSize = ViewBag.PageSize;
    SelectList pageSizeDDL = ViewBag.PageSizeDDL;

    int index = Model.FirstItemOnPage;
    string returnListUrl = Request.RawUrl;

    ViewBag.Title = "Quản lý xu hướng " + trend.Name;
}

<section class="content-header">
    <h1>
        Xu hướng
        <small>@trend.Name</small>
    </h1>
    <ol class="breadcrumb">
        <li>
            <a href="@Url.Action("Index", "Home")">
                <i class="fa fa-home"></i> Trang chủ
            </a>
        </li>
        <li>
            <a href="@Url.Action("Index", "Trend")">
                Danh sách xu hướng
            </a>
        </li>
        <li class="active">@trend.Name</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-6">
            <div class="box box-solid">
                <div class="box-body">
                    @using (Html.BeginForm("Manage", "Trend", FormMethod.Get))
                    {
                        @Html.Hidden("Id", trend.Id)
                        @Html.Hidden("SortOrder", sortOrder)

                        <div class="row">
                            <div class="col-md-5">
                                @Html.DropDownList("PageSize", pageSizeDDL, new
                                {
                                    @class = "form-control",
                                    onchange = "this.form.submit()"
                                })
                            </div>
                            <div class="col-md-7">
                                <div class="input-group">
                                    @Html.TextBox("Search", search, new
                                    {
                                        @class = "form-control",
                                        name = "search",
                                        placeholder = "Tên sản phẩm"
                                    })
                                    <span class="input-group-btn">
                                        <button type="submit" class="btn btn-primary">
                                            <i class="fa fa-search"></i>
                                        </button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    }
                    @using (Html.BeginForm("Manage", "Trend", FormMethod.Post))
                    {
                        @Html.Hidden("TrendId", trend.Id)
                        @Html.Hidden("ReturnListUrl", returnListUrl)
                        @Html.Hidden("Command", "Add")

                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <td colspan="6" class="text-center">
                                            <button type="submit" class="btn btn-success">
                                                <i class="fa fa-plus fa-mr"></i> Thêm vào sự kiện
                                            </button>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th width="60">#</th>
                                        <th width="50"></th>
                                        <th>
                                            @Html.Raw(SortOrderLink("Tên", "Name", search, pageSize, trend.Id))
                                        </th>
                                        <th width="110">
                                            @Html.Raw(SortOrderLink("Đơn giá", "ListPrice", search, pageSize, trend.Id))
                                        </th>
                                        <th width="50"></th>
                                        <th width="50" class="text-center">
                                            <input type="checkbox" id="CheckAll" />
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (Product item in Model)
                                    {
                                        Image image = item.Images.Where(x => !x.IsDelete)
                                            .OrderByDescending(x => x.Priority)
                                            .FirstOrDefault();
                                        string thumbUrl = "/img/no-image.png?w=50&h=50";
                                        bool isStandard = false;
                                        if (image != null)
                                        {
                                            if (ImageHelper.IsStandard(image.Width, image.Height))
                                            {
                                                thumbUrl = image.Url + "?w=50";
                                                isStandard = true;
                                            }
                                            else
                                            {
                                                thumbUrl = image.Url + "?w=50&h=50";
                                            }
                                        }

                                        float minPrice = 0;
                                        float maxPrice = 0;
                                        if (item.Styles.Count > 0)
                                        {
                                            minPrice = item.Styles.Min(x => x.UnitPrice);
                                            maxPrice = item.Styles.Max(x => x.UnitPrice);
                                        }

                                        <tr>
                                            <td>
                                                @(index++)
                                            </td>
                                            <td>
                                                <img class="center-block @(isStandard == true ? "" : "no-standard")" src="@thumbUrl" />
                                            </td>
                                            <td>
                                                <div>@item.Name</div>
                                                <div>@item.Code</div>
                                            </td>
                                            <td class="text-right">
                                                <div class="text-danger">
                                                    @StringHelper.PriceFormat(item.ListPrice, "usd")
                                                </div>
                                                <div class="text-green">
                                                    @if (minPrice == maxPrice)
                                                    {
                                                        <span>@StringHelper.PriceFormat(minPrice, "usd")</span>
                                                    }
                                                    else
                                                    {
                                                        <span>@StringHelper.PriceFormat(minPrice, "usd") - @StringHelper.PriceFormat(maxPrice, "usd")</span>
                                                    }
                                                </div>
                                            </td>
                                            <td class="control">
                                                <a href="@Url.Action("Details", "Product", new { id = item.Id, returnListUrl = returnListUrl })"
                                                   data-toggle="tooltip" data-placement="top" title="Chi tiết">
                                                    <i class="fa fa-info"></i>
                                                </a>
                                            </td>
                                            <td class="text-center">
                                                <input type="checkbox" class="cbProduct" name="ProductIds" value="@item.Id" />
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="6" class="text-center">
                                            <button type="submit" class="btn btn-success">
                                                <i class="fa fa-plus fa-mr"></i> Thêm vào sự kiện
                                            </button>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    }
                    <div class="row">
                        <div class="col-md-4">
                            <button type="button" class="btn btn-default" disabled="disabled">
                                @Model.FirstItemOnPage - @Model.LastItemOnPage / @Model.TotalItemCount
                            </button>
                        </div>
                        <div class="col-md-8 text-right">
                            @Html.PagedListPager(Model, page => Url.Action("Manage", new
                            {
                                page,
                                sortOrder = sortOrder,
                                search = search,
                                pageSize = pageSize,
                                id = trend.Id
                            }), new PagedListRenderOptions
                            {
                                LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                                LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                                LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                                LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                                MaximumPageNumbersToDisplay = 5
                            })
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="box box-solid">
                <div class="box-body">
                    <button type="button" class="btn btn-primary btn-block" disabled="disabled">
                        Danh sách sản phẩm trong sự kiện
                    </button>
                    @using (Html.BeginForm("Manage", "Trend", FormMethod.Post))
                    {
                        @Html.Hidden("TrendId", trend.Id)
                        @Html.Hidden("ReturnListUrl", returnListUrl)
                        @Html.Hidden("Command", "Remove")

                        int index2 = 1;

                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <td colspan="6" class="text-center">
                                            <button type="submit" class="btn btn-warning">
                                                <i class="fa fa-times fa-mr"></i> Xóa khỏi sự kiện
                                            </button>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th width="60">#</th>
                                        <th width="50"></th>
                                        <th>
                                            Tên
                                        </th>
                                        <th width="110" class="text-right">
                                            Đơn giá
                                        </th>
                                        <th width="50"></th>
                                        <th width="50" class="text-center">
                                            <input type="checkbox" id="CheckAll2" />
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (Product item in trend.TrendProducts.Select(x => x.Product))
                                    {
                                        Image image = item.Images.Where(x => !x.IsDelete)
                                            .OrderByDescending(x => x.Priority)
                                            .FirstOrDefault();
                                        string thumbUrl = "/img/no-trend.png?w=50&h=50";
                                        bool isStandard = false;
                                        if (image != null)
                                        {
                                            if (ImageHelper.IsStandard(image.Width, image.Height))
                                            {
                                                thumbUrl = image.Url + "?w=50";
                                                isStandard = true;
                                            }
                                            else
                                            {
                                                thumbUrl = image.Url + "?w=50&h=50";
                                            }
                                        }

                                        float minPrice = 0;
                                        float maxPrice = 0;
                                        if (item.Styles.Count > 0)
                                        {
                                            minPrice = item.Styles.Min(x => x.UnitPrice);
                                            maxPrice = item.Styles.Max(x => x.UnitPrice);
                                        }

                                        <tr>
                                            <td>
                                                @(index2++)
                                            </td>
                                            <td>
                                                <img class="center-block @(isStandard == true ? "" : "no-standard")" src="@thumbUrl" />
                                            </td>
                                            <td>
                                                <div>@item.Name</div>
                                                <div>@item.Code</div>
                                            </td>
                                            <td class="text-right">
                                                <div class="text-danger">
                                                    @StringHelper.PriceFormat(item.ListPrice, "usd")
                                                </div>
                                                <div class="text-green">
                                                    @if (minPrice == maxPrice)
                                                    {
                                                        <span>@StringHelper.PriceFormat(minPrice, "usd")</span>
                                                    }
                                                    else
                                                    {
                                                        <span>@StringHelper.PriceFormat(minPrice, "usd") - @StringHelper.PriceFormat(maxPrice, "usd")</span>
                                                    }
                                                </div>
                                            </td>
                                            <td class="control">
                                                <a href="@Url.Action("Details", "Product", new { id = item.Id, returnListUrl = returnListUrl })"
                                                   data-toggle="tooltip" data-placement="top" title="Chi tiết">
                                                    <i class="fa fa-info"></i>
                                                </a>
                                            </td>
                                            <td class="text-center">
                                                <input type="checkbox" class="cbProduct2" name="ProductIds" value="@item.Id" />
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="6" class="text-center">
                                            <button type="submit" class="btn btn-warning">
                                                <i class="fa fa-times fa-mr"></i> Xóa khỏi sự kiện
                                            </button>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</section>

@section scripts {
    <script>
        ActiveMenu('#trend-menu');

        $('#CheckAll').on('ifChecked', function () {
            $('.cbProduct').iCheck('check');
        });

        $('#CheckAll').on('ifUnchecked', function () {
            $('.cbProduct').iCheck('uncheck');
        });

        $('#CheckAll2').on('ifChecked', function () {
            $('.cbProduct2').iCheck('check');
        });

        $('#CheckAll2').on('ifUnchecked', function () {
            $('.cbProduct2').iCheck('uncheck');
        });
    </script>
}

@functions {
    private string SortOrderLink(string displayName, string name, string search, int pageSize, int id)
    {
        string url = Url.Action("Manage", new
        {
            sortOrder = ViewData[name + "Sort"],
            search = search,
            pageSize = pageSize,
            id = id
        });

        string icon = "";

        if (!String.IsNullOrWhiteSpace(ViewData[name + "Icon"] as String))
        {
            icon = ViewData[name + "Icon"] as String;
        }

        return "<a href='" + url + "'>" + displayName + icon + "</a>";
    }
}